package com.star.exception;

import ch.qos.logback.classic.Logger;
import com.star.utils.RestResult;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalException {
    private Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(value = MyException.class)
    public Object test(MyException myex) {

        RestResult restResult = new RestResult(false, myex.getCode(), null, myex.getMessage());
        return restResult;
    }


    @ExceptionHandler(value = Exception.class)
    public Object exception(Exception e) {

        logger.error("系统异常：{}",e.getMessage());
        e.printStackTrace();
        RestResult restResult = new RestResult(false, "500", null, e.getMessage());
        return restResult;
    }

}
